package cn.dakaqi.utils.sms.taobao;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

import java.util.Random;

/**
 * Created by chunyang on 2015/11/19.
 */
public class TaobaoSMS
{
    private static final String productName = "志愿者打卡器";
    private static final String productUrl = "http://gw.api.taobao.com/router/rest";
    private static final String taobaoAppKey = "23266570";
    private static final String taobaoSecret = "c71dc12207aa9016ba5678c322001ec6";
    static TaobaoClient client = null;
    static
    {
        if(null == client)
            client = new DefaultTaobaoClient(productUrl, taobaoAppKey, taobaoSecret);
    }
    private static void testTaobaoSMS()
    {

        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setExtend("15821117931");
        req.setSmsType("normal");
        req.setSmsFreeSignName("注册验证");
        req.setSmsParam("{\"code\":\"123456\",\"product\":\"志愿者打卡器\"}");
        req.setRecNum("15821117931");
        req.setSmsTemplateCode("SMS_2185729");
        try
        {

            //AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req, sessionKey);
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
            System.out.println(rsp.getBody());
        } catch (ApiException e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 注册时手机验证码
     * @param userPhone
     * @param randCode
     * @return
     * @throws Exception
     */
    public static boolean sendRegVerificationCode(String userPhone,String randCode)throws Exception
    {
        boolean F = false;
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setExtend(userPhone);
        req.setSmsType("normal");
        req.setSmsFreeSignName("注册验证");
        req.setSmsParam("{\"code\":\""+randCode+"\",\"product\":\""+productName+"\"}");
        req.setRecNum(userPhone);
        req.setSmsTemplateCode("SMS_2185729");
        try
        {
            //AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req, sessionKey);
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
            String result = rsp.getBody();
            if(result.contains("\"success\":true"))
                F = true;
            System.out.println(result);
        } catch (ApiException e)
        {
            e.printStackTrace();
        }
        return F;
    }
    /**
     * 找回手机密码验证码
     * @param userPhone
     * @param randCode
     * @returnPassword
     * @throws Exception
     */
    public static boolean sendFindVerificationCode(String userPhone,int randCode)throws Exception
    {
        boolean F = false;
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setExtend(userPhone);
        req.setSmsType("normal");
        req.setSmsFreeSignName("身份验证");
        req.setSmsParam("{\"code\":\""+randCode+"\",\"product\":\""+productName+"\"}");
        req.setRecNum(userPhone);
        req.setSmsTemplateCode("SMS_2185727");
        try
        {
            //AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req, sessionKey);
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
            String result = rsp.getBody();
            //JSONObject jsonObject = JSONObject.fromObject(result);
            //{"alibaba_aliqin_fc_sms_num_send_response":{"result":{"model":"100279803045^1100486319935","success":true},"request_id":"15q9o7dq058q6"}}
            if(result.contains("\"success\":true"))
                F = true;
            System.out.println(result);
        } catch (ApiException e)
        {
            e.printStackTrace();
        }
        return F;
    }
    public static void main(String[] args)
    {
        try
        {
            System.out.println(sendRegVerificationCode("15821117931", get6Rand() + ""));
            //sendFindVerificationCode("15821117931", get6Rand());
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private static int get4Rand()
    {
        Random rand = new Random();
        int tmp = Math.abs(rand.nextInt());
        return tmp % (9999 - 1000 + 1) + 1000;
    }
    private static int get6Rand()
    {
        Random rand = new Random();
        int tmp = Math.abs(rand.nextInt());
        return tmp % (999999 - 100000 + 1) + 100000;
    }

}
